package com.example.tests.framework;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;	}

	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> lastnames = driver.findElements(By.xpath("//tr[@name='entry']/td[2]"));
		for (WebElement contactname : lastnames) {
			String lname = contactname.getText();
			cachedContacts.add(new ContactData().withLname(lname));
		}
	}

	public ContactHelper createContact(ContactData contact, boolean CREATION) {
		manager.navigateTo().mainPage();
		initNewContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		manager.navigateTo().returnToHomePage();
		rebuildCache();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact, boolean MODIFICATION) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		manager.navigateTo().returnToHomePage();
		rebuildCache();
		return this;
		}

	public ContactHelper removeContact(int i) {
		manager.navigateTo().mainPage();
		initContactModification(i);
		deleteContact();
		manager.navigateTo().returnToHomePage();
		rebuildCache();
		return this;
	}
	
//------------------------------------------------------------------------------------
	
	public ContactHelper initNewContactCreation() {
		click(By.linkText("add new"));
		return this;
		}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		input(By.name("firstname"), contact.getFname());
		input(By.name("lastname"), contact.getLname());
		input(By.name("address"), contact.getPrimaddress());
		input(By.name("home"), contact.getPrimhomephone());
		input(By.name("mobile"), contact.getMobphone());
		input(By.name("work"), contact.getWorkphone());
		input(By.name("email"), contact.getEmailprim());
		input(By.name("email2"), contact.getEmailsec());
		selectValue(By.name("bday"), contact.getBirthday());
		selectValue(By.name("bmonth"), contact.getBirthmonth());
		input(By.name("byear"), contact.getBirthyear());
		if (formType == CREATION) {
		//	selectValue(By.name("new_group"), contact.contactgroup);
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0) {
				throw new Error ("Group selector found in contact modification form");
			}
		}
		input(By.name("address2"), contact.getSecaddress());
		input(By.name("phone2"), contact.getSechomephone());
		return this;
		}
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
		}

	public ContactHelper initContactModification(int index) {
		index = index + 2; //since the first table row is the grid header
		click(By.xpath("//tr[" + index + "]//img[@alt='Edit']"));
		return this;
		}	

	public ContactHelper deleteContact() {
		click(By.xpath("//input[@value='Delete']"));
		cachedContacts = null;
		return this;
		}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value='Update']"));
		cachedContacts = null;
		return this;
		}

}